package parser;

public class PFragment {
//attribution
	public short fragmentType;
	public String tableName;
	public int fragmentationNum;
	public String []fragmentNames;
	public String []siteNames;
	public String []conditionClauses;
//method
	PFragment(String cmd){
		if(cmd.contains("HORIZONTAL")){
			parseHorizontal(cmd);
			fragmentType = PConstants.HORIZONTAL;
			//System.out.println("");
		}
		else if(cmd.contains("VERTICAL")){
			parseVertical(cmd);
			fragmentType = PConstants.VETICAL;
			//System.out.println("");
		}
		else System.out.println("type error!");
	}
	
	void parseHorizontal(String cmd){
		String []sAry;
		String []ss;
		sAry = cmd.split("\\s+WITH\\s+");
		ss = sAry[0].split("\\s+");
		tableName = ss[2];
		ss = sAry[1].split("\\s*\\,\\s+");
		fragmentationNum = ss.length;
		fragmentNames = new String[fragmentationNum];
		siteNames = new String[fragmentationNum];
		conditionClauses = new String[fragmentationNum];
		int pos;
		for(int i=0; i<fragmentationNum; i++){
			sAry = ss[i].split("\\s+TO\\s+");
			siteNames[i] = sAry[1];
			pos = sAry[0].indexOf('(');	
			fragmentNames[i] = sAry[0].substring(0,pos);
			sAry[0] = sAry[0].substring(pos+1,sAry[0].length()-1);
			conditionClauses[i] = sAry[0];
		}
		//System.out.println("");
	}
	
	void parseVertical(String cmd){
		String []sAry;
		String []ss;
		sAry = cmd.split("\\s+WITH\\s+");
		ss = sAry[0].split("\\s+");
		tableName = ss[2];
		ss = sAry[1].split("\\s*\\,\\s+");
		fragmentationNum = ss.length;
		fragmentNames = new String[fragmentationNum];
		siteNames = new String[fragmentationNum];
		conditionClauses = new String[fragmentationNum];
		int pos;
		for(int i=0; i<fragmentationNum; i++){
			sAry = ss[i].split("\\s+TO\\s+");
			siteNames[i] = sAry[1];
			pos = sAry[0].indexOf('(');	
			fragmentNames[i] = sAry[0].substring(0,pos);
			sAry[0] = sAry[0].substring(pos+1,sAry[0].length()-1);
			conditionClauses[i] = sAry[0];
		}
		//System.out.println("");
	}
}
